home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / hppab-na.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  8.4 KB  |  329 lines

  1. /* Machine-dependent hooks for the unix child process stratum.  This
  2.    code is for the HP PA-RISC cpu.
  3.  
  4.    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  5.  
  6.    Contributed by the Center for Software Science at the
  7.    University of Utah (pa-gdb-bugs@cs.utah.edu).
  8.  
  9. This file is part of GDB.
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  24.  
  25. #include "defs.h"
  26. #include "inferior.h"
  27. #include "target.h"
  28. #include <sys/ptrace.h>
  29.  
  30. #ifndef PT_ATTACH
  31. #define PT_ATTACH PTRACE_ATTACH
  32. #endif
  33.  
  34. #ifndef PT_DETACH
  35. #define PT_DETACH PTRACE_DETACH
  36. #endif
  37.  
  38. /* This function simply calls ptrace with the given arguments.  
  39.    It exists so that all calls to ptrace are isolated in this 
  40.    machine-dependent file. */
  41.  
  42. int
  43. call_ptrace (request, pid, addr, data)
  44.      int request, pid;
  45.      PTRACE_ARG3_TYPE addr;
  46.      int data;
  47. {
  48.   return ptrace (request, pid, addr, data, 0);
  49. }
  50.  
  51. /* Use an extra level of indirection for ptrace calls.
  52.    This lets us breakpoint usefully on call_ptrace.   It also
  53.    allows us to pass an extra argument to ptrace without
  54.    using an ANSI-C specific macro.  */
  55.  
  56. #define ptrace call_ptrace
  57.  
  58. void
  59. kill_inferior ()
  60. {
  61.   if (inferior_pid == 0)
  62.     return;
  63.   ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
  64.   wait ((int *)0);
  65.   target_mourn_inferior ();
  66. }
  67.  
  68. #ifdef ATTACH_DETACH
  69.  
  70. /* Start debugging the process whose number is PID.  */
  71. int
  72. attach (pid)
  73.      int pid;
  74. {
  75.   errno = 0;
  76.   ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
  77.   if (errno)
  78.     perror_with_name ("ptrace");
  79.   attach_flag = 1;
  80.   return pid;
  81. }
  82.  
  83. /* Stop debugging the process whose number is PID
  84.    and continue it with signal number SIGNAL.
  85.    SIGNAL = 0 means just continue it.  */
  86.  
  87. void
  88. detach (signal)
  89.      int signal;
  90. {
  91.   errno = 0;
  92.   ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
  93.   if (errno)
  94.     perror_with_name ("ptrace");
  95.   attach_flag = 0;
  96. }
  97. #endif /* ATTACH_DETACH */
  98.  
  99.  
  100.  
  101. #if !defined (offsetof)
  102. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  103. #endif
  104.  
  105. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  106. #if !defined (U_REGS_OFFSET)
  107. #define U_REGS_OFFSET \
  108.   ptrace (PT_READ_U, inferior_pid, \
  109.           (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
  110.     - KERNEL_U_ADDR
  111. #endif
  112.  
  113. /* Fetch one register.  */
  114.  
  115. static void
  116. fetch_register (regno)
  117.      int regno;
  118. {
  119.   register unsigned int regaddr;
  120.   char buf[MAX_REGISTER_RAW_SIZE];
  121.   register int i;
  122.  
  123.   /* Offset of registers within the u area.  */
  124.   unsigned int offset;
  125.  
  126.   offset = U_REGS_OFFSET;
  127.  
  128.   regaddr = register_addr (regno, offset);
  129.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  130.     {
  131.       errno = 0;
  132.       *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
  133.                  (PTRACE_ARG3_TYPE) regaddr, 0);
  134.       regaddr += sizeof (int);
  135.       if (errno != 0)
  136.     {
  137.       /* Warning, not error, in case we are attached; sometimes the
  138.          kernel doesn't let us at the registers.  */
  139.       char *err = safe_strerror (errno);
  140.       char *msg = alloca (strlen (err) + 128);
  141.       sprintf (msg, "reading register %s: %s", reg_names[regno], err);
  142.       warning (msg);
  143.       goto error_exit;
  144.     }
  145.     }
  146.   supply_register (regno, buf);
  147.  error_exit:;
  148. }
  149.  
  150. /* Fetch all registers, or just one, from the child process.  */
  151.  
  152. void
  153. fetch_inferior_registers (regno)
  154.      int regno;
  155. {
  156.   if (regno == -1)
  157.     for (regno = 0; regno < NUM_REGS; regno++)
  158.       fetch_register (regno);
  159.   else
  160.     fetch_register (regno);
  161. }
  162.  
  163. /* Store our register values back into the inferior.
  164.    If REGNO is -1, do this for all registers.
  165.    Otherwise, REGNO specifies which register (so we can save time).  */
  166.  
  167. void
  168. store_inferior_registers (regno)
  169.      int regno;
  170. {
  171.   register unsigned int regaddr;
  172.   extern char registers[];
  173.   register int i;
  174.  
  175.   unsigned int offset = U_REGS_OFFSET;
  176.  
  177.   if (regno >= 0)
  178.     {
  179.       regaddr = register_addr (regno, offset);
  180.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  181.     {
  182.       errno = 0;
  183.       ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
  184.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  185.       if (errno != 0)
  186.         {
  187.           char *err = safe_strerror (errno);
  188.           char *msg = alloca (strlen (err) + 128);
  189.           sprintf (msg, "writing register %s: %s", reg_names[regno], err);
  190.           warning (msg);
  191.         }
  192.       regaddr += sizeof(int);
  193.     }
  194.     }
  195.   else
  196.     {
  197.       for (regno = 0; regno < NUM_REGS; regno++)
  198.     {
  199.       if (CANNOT_STORE_REGISTER (regno))
  200.         continue;
  201.       store_inferior_registers (regno);
  202.     }
  203.     }
  204.   return;
  205. }
  206.  
  207. /* Resume execution of process PID.
  208.    If STEP is nonzero, single-step it.
  209.    If SIGNAL is nonzero, give it that signal.  */
  210.  
  211. void
  212. child_resume (pid, step, signal)
  213.      int pid;
  214.      int step;
  215.      enum target_signal signal;
  216. {
  217.   errno = 0;
  218.  
  219.   if (pid == -1)
  220.     pid = inferior_pid;
  221.  
  222.   /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
  223.      it was. (If GDB wanted it to start some other way, we have already
  224.      written a new PC value to the child.)  */
  225.  
  226.   if (step)
  227.     ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
  228.         target_signal_to_host (signal));
  229.   else
  230.     ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
  231.         target_signal_to_host (signal));
  232.  
  233.   if (errno)
  234.     perror_with_name ("ptrace");
  235. }
  236.  
  237. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  238.    in the NEW_SUN_PTRACE case.
  239.    It ought to be straightforward.  But it appears that writing did
  240.    not write the data that I specified.  I cannot understand where
  241.    it got the data that it actually did write.  */
  242.  
  243. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  244.    to debugger memory starting at MYADDR.   Copy to inferior if
  245.    WRITE is nonzero.
  246.   
  247.    Returns the length copied, which is either the LEN argument or zero.
  248.    This xfer function does not do partial moves, since child_ops
  249.    doesn't allow memory operations to cross below us in the target stack
  250.    anyway.  */
  251.  
  252. int
  253. child_xfer_memory (memaddr, myaddr, len, write, target)
  254.      CORE_ADDR memaddr;
  255.      char *myaddr;
  256.      int len;
  257.      int write;
  258.      struct target_ops *target;        /* ignored */
  259. {
  260.   register int i;
  261.   /* Round starting address down to longword boundary.  */
  262.   register CORE_ADDR addr = memaddr & - sizeof (int);
  263.   /* Round ending address up; get number of longwords that makes.  */
  264.   register int count
  265.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  266.   /* Allocate buffer of that many longwords.  */
  267.   register int *buffer = (int *) alloca (count * sizeof (int));
  268.  
  269.   if (write)
  270.     {
  271.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  272.  
  273.       if (addr != memaddr || len < (int)sizeof (int)) {
  274.     /* Need part of initial word -- fetch it.  */
  275.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  276.                 0);
  277.       }
  278.  
  279.       if (count > 1)        /* FIXME, avoid if even boundary */
  280.     {
  281.       buffer[count - 1]
  282.         = ptrace (PT_READ_I, inferior_pid,
  283.               (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
  284.               0);
  285.     }
  286.  
  287.       /* Copy data to be written over corresponding part of buffer */
  288.  
  289.       memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  290.  
  291.       /* Write the entire buffer.  */
  292.  
  293.       for (i = 0; i < count; i++, addr += sizeof (int))
  294.     {
  295.       errno = 0;
  296.       ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  297.           buffer[i]);
  298.       if (errno)
  299.         {
  300.           /* Using the appropriate one (I or D) is necessary for
  301.          Gould NP1, at least.  */
  302.           errno = 0;
  303.           ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  304.               buffer[i]);
  305.         }
  306.       if (errno)
  307.         return 0;
  308.     }
  309.     }
  310.   else
  311.     {
  312.       /* Read all the longwords */
  313.       for (i = 0; i < count; i++, addr += sizeof (int))
  314.     {
  315.       errno = 0;
  316.       buffer[i] = ptrace (PT_READ_I, inferior_pid,
  317.                   (PTRACE_ARG3_TYPE) addr, 0);
  318.       if (errno)
  319.         return 0;
  320.       QUIT;
  321.     }
  322.  
  323.       /* Copy appropriate bytes out of the buffer.  */
  324.       memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  325.     }
  326.   return len;
  327. }
  328.  
  329.